package com.czxy.demo23;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

public class Test01 {
    public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, IOException, InstantiationException, NoSuchMethodException, ClassNotFoundException {
        List<Animal> show = show();
        for (Animal animal : show) {
            System.out.println(animal);
        }
    }
    public static List<Animal> show() throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, InvocationTargetException {
        List<Animal> list = new ArrayList<>();
        List<String> lines = FileUtils.readLines(new File("dataAnimal.txt"), "UTF-8");
        for (String line : lines) {
            String[] ss = line.split(" ");
            Class<?> aClass = Class.forName(ss[0]);
            if (ss[0].endsWith("Dog")){
                Constructor<?> con = aClass.getConstructor(String.class, String.class, int.class);
                Object o = con.newInstance(getValue(ss[1]), getValue(ss[2]), Integer.parseInt(getValue(ss[3])));
                list.add((Dog) o);

            }else if (ss[0].endsWith("Cat")){
                Constructor<?> con = aClass.getConstructor(String.class, String.class, String.class);
                Object o = con.newInstance(getValue(ss[1]), getValue(ss[2]), getValue(ss[3]));
                list.add((Cat) o);
            }
        }
        return list;
    }
    public static String getValue(String s){
        return s.substring(s.indexOf("=")+1);
    }
}
